// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// E [author] rhyme_lph
// v [email]  rhymelph@gmail.com
// B [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/http_service/http_category_service.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/dashboard/select/select_category_page.dart';

class SelectCategoryProvider extends BaseProvider<List<SettingDataCategory>> {
  SettingDataCategory currentSelectCode;
  SettingDataCurrency unit;
  String type;

  List<int> recentIds;

  bool isPop = false;

  @override
  FutureOr<List<SettingDataCategory>> transformData(
      List<SettingDataCategory> d, bool isCache) async {
    if (type == 'cash out') {
      RProvider.myProvider.globalMap[ConfigKey.cashOutCategory] = d;
      recentIds = Config.cashOutRecentIds;
    } else if (type == 'cash in') {
      RProvider.myProvider.globalMap[ConfigKey.cashInCategory] = d;
      recentIds = Config.cashInRecentIds;
    } else if (type == 'expense') {
      RProvider.myProvider.globalMap[ConfigKey.cashInCategory] = d;
      recentIds = Config.expenseRecentIds;
    }

    await RProvider.myProvider.updateGlobalMap();
    return super.transformData(d, isCache);
  }

  @override
  Future get dataFuture {
    if (type == 'expense') {
      return HttpCategoryService.expenseCategories();
    } else if (type == 'cash out') {
      return HttpCategoryService.cashOutCategories();
    } else if (type == 'cash in') {
      return HttpCategoryService.cashInCategories();
    }
    return HttpCategoryService.categories();
  }

  void onChangeSelect(SettingDataCategory value) {
    if (currentSelectCode == value) return;
    currentSelectCode = value;
    updateRecentUsedIds(value.id);
    update();
    if (isPop) {
      obtainContext?.call((context) {
        Navigator.of(context).pop(currentSelectCode);
      });
    } else {
      Routes.navigateTo(RouterGenProviders.amountGstRateCategoryPage, params: {
        'category': currentSelectCode,
        'type': type,
        'unit': unit,
      });
    }
  }

  void updateRecentUsedIds(int id) async {
    if (recentIds == null) {
      recentIds = [id];
    } else {
      recentIds.remove(id);
      recentIds.insert(0, id);
      if (recentIds.length > 5) {
        recentIds.removeLast();
      }
    }
    if (type == 'expense') {
      RProvider.myProvider.globalMap[ConfigKey.expenseRecentIds] = recentIds;
    } else if (type == 'cash out') {
      RProvider.myProvider.globalMap[ConfigKey.cashOutRecentIds] = recentIds;
    } else if (type == 'cash in') {
      RProvider.myProvider.globalMap[ConfigKey.cashInRecentIds] = recentIds;
    }
    await RProvider.myProvider.updateGlobalMap();
  }
}
